package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.common.pojo.protocol.JsonResult;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceClient;
import cn.tedu.micro.demo.order.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private DeviceClient deviceClient;//注射
    @Override
    public String orderSubmit(Integer gunId, Integer userId) {
            JsonResult<GunInfoVO> result = deviceClient.gunDetail(gunId.longValue());
            GunInfoVO gunInfoVO = result.getData();
            if(gunInfoVO!=null&&gunInfoVO.getStatus()==1){//判断用户生成订单时 ，当时这把抢的状态是否时空闲
              //  po想eneity  dang
                OrderInfoPO order=new OrderInfoPO();
                //2.填充完毕po所有属性 调用新增
                order.setGunId(gunId.longValue());
                order.setUserId(userId.longValue());
                //关于订单编号 保证1 唯一  2 有序  3 可读 比如身份证110111199909100981
                order.setBillId(UUID.randomUUID().toString());
                order.setCreateTime(new Date());
                order.setUpdateTime(new Date());
                //3.调用仓储层 新增
                orderRepository.insert(order);
                return order.getBillId();
            }else{
                return null;
            }
//
    }
}
